package cn.itsource.order.service.impl;



import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.LoginContext;
import cn.itsource.basic.util.PageList;
import cn.itsource.order.domain.OrderPetAcquisition;
import cn.itsource.order.dto.OrderDto;
import cn.itsource.order.mapper.OrderPetAcquisitionMapper;
import cn.itsource.order.query.OrderPetAcquisitionQuery;
import cn.itsource.order.service.IOrderPetAcquisitionService;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.SearchMasterMsg;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.SearchMasterMsgMapper;
import cn.itsource.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;

    @Override
    public PageList<OrderPetAcquisition> queryPage(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        Logininfo logininfo = LoginContext.getLogininfo(request);
        Employee employee = employeeMapper.findByLogininfoId(logininfo.getId());
        //有shop_id就是店铺管理员
        if (employee.getShop_id() != null){
            Shop shop = shopMapper.findOne(employee.getShop_id());
            if (!shop.getAdmin_id().equals(employee.getId())){
                //否则就是店员
                query.setEmpId(employee.getId());
            }
            //通过shop_id查询店铺得到admin_id如果为当前登录管理员的id，就为店铺管理员 - 店长
            query.setShopId(shop.getId());
        }
        //没有shop_id就是平台管理员
        return super.queryPage(query);
    }

    /** 取消订单 */
    @Override
    public AjaxResult cancel(Long id) {
        //将寻主消息放入寻主池：state=3，shopId=nul
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(id);
        searchMasterMsgMapper.updateState(order.getSearchMasterMsgId());
        //修改订单状态为-1
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
        return new AjaxResult();
    }

    /** 确认订单 */
    @Override
    public AjaxResult confirm(OrderDto orderDto) {
        //确认订单要添加宠物
        OrderPetAcquisition order = orderPetAcquisitionMapper.findOne(orderDto.getId());
        SearchMasterMsg msg = searchMasterMsgMapper.findOne(order.getSearchMasterMsgId());
        Pet pet = msg2Pet(msg,orderDto);
        petMapper.add(pet);

        //2.改订单状态为 1，价格为成交价，支付方式
        order.setState(1);
        order.setPrice(orderDto.getMoney());
        order.setPaytype(orderDto.getPayType());
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);

        //3.改寻主消息状态 - 2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);
        return new AjaxResult();
    }

    private Pet msg2Pet(SearchMasterMsg msg, OrderDto orderDto) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(orderDto.getMoney());
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        pet.setSearchMasterMsgId(msg.getId());
        return pet;
    }
}
